﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using Triunfadores.BusinessEntities;
using Triunfadores.BusinessLogic.Service;

namespace Triunfadores.Web.Acciones
{
    public partial class Acciones : System.Web.UI.Page
    {
        [Dependency]
        public IAccionService AccionService { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            var opc = Request.QueryString["opc"];
            var idAccion = Request.QueryString["idAccion"];

            fvAccion.DefaultMode = FormViewMode.Insert;

            if (opc != null && opc == "nuevo")
            {
                fvAccion.DefaultMode = FormViewMode.Insert;
                upAcciones.Visible = false;
            }
            if (opc != null && opc == "editar")
            {
                fvAccion.DefaultMode = FormViewMode.Edit;
                upAcciones.Visible = false;
            }
        }

        public IEnumerable<Accion> ObtenerAcciones()
        {
            return AccionService.ObtenerTodasLasAcciones();
        }

        public Accion ObtenerAccionPorId([QueryString("idAccion")] Int32? id)
        {
            if (id.HasValue)
            {
                return AccionService.ObtenerAccionPorId(id.Value);
            }
            return null;
        }

        public void InsertAccion(Accion accion)
        {
            //if (ModelState.IsValid)
            //{
            AccionService.AddAccion(accion);

            Response.Redirect("Acciones.aspx");
            //}
        }

        public void UpdateAccion(Accion accion)
        {
            if (ModelState.IsValid)
            {
                AccionService.UpdateAccion(accion);

                Response.Redirect("Acciones.aspx");
            }
        }

        public void DeleteAccion(Int32 id)
        {
            AccionService.DeleteAccion(id);
            Response.Redirect("Acciones.aspx");
        }

    }
}